﻿using Api.Application.Command.Leave;
using AutoMapper;
using Domain;
using ErrorCode;
using Infrastructure;
using MediatR;

namespace Api.Application.Handler.Leave
{
    public class DelLeaveHandler : IRequestHandler<DelLeaveCommand, ApiResult<int>>
    {
        private readonly IBase<UserModel> userRepo;
        private readonly IBase<LeaveModel> leaveRepo;
        private readonly IMapper mapper;

        public DelLeaveHandler(IBase<UserModel> userRepo,IBase<LeaveModel> leaveRepo,IMapper mapper)
        {
            this.userRepo = userRepo;
            this.leaveRepo = leaveRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<int>> Handle(DelLeaveCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var leave = leaveRepo.GetAll().Where(x => x.Id==request.Id).FirstOrDefault();
            leave.IsDeleted = true;
            var r=leaveRepo.Update(leave).Result;
            if(r>0)
            {
                result.Code = ApiEnum.OK;
                result.Data = r;
                result.Msg = "请假审批删除成功";
            }
            else
            {
                result.Code = ApiEnum.Fail;
                result.Data = r;
                result.Msg = "请假审批删除失败";
            }
            return Task.FromResult(result);
        }
    }
}
